<?php


class Offer {

    public function list_active_offers() {
        $db_obj = new DB();
        $offer_sql = "SELECT *
                        FROM offer
                       WHERE offer_available='Y'
                         AND offer_end_date > now()   and maximum_purchases>0";
        $db_obj->query($offer_sql);
        if($db_obj->rowCount() > 0) {
            $offer_details = $db_obj->getResults($offer_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($offer_details);
        } else {
            return false;
        }
    }
     public function is_active_offer($offer_prefix) {
        $db_obj = new DB();
        $offer_sql = "SELECT *
                        FROM offer
                       WHERE offer_prefix='".$offer_prefix."' and offer_available='Y' 
                         AND offer_end_date > now() and maximum_purchases>0";
        $db_obj->query($offer_sql);
        if($db_obj->rowCount() > 0) {
            
            return true;
        } else {
            return false;
        }
    }

    public function list_inactive_offers() {
        $db_obj = new DB();
        $offer_sql = "SELECT *
                        FROM offer
                       WHERE offer_available='N'";
        $db_obj->query($offer_sql);
        if($db_obj->rowCount() > 0) {
            $offer_details = $db_obj->getResults($offer_sql);
            $extra_obj = new Extra();
            return $extra_obj->objectToArray($offer_details);
        } else {
            return false;
        }
    }

    public function get_noof_sold_items($offer_id) {
        $db_obj = new DB();
        $sold_item_sql = "SELECT count(order_id) as item_sold
                        FROM offer_orders 
                       WHERE offer_id=".$offer_id;
        $extra_obj = new Extra();
        $sold_item = $extra_obj->objectToArray($db_obj->getRow($sold_item_sql));
        return $sold_item['item_sold'];
    }

    public function get_offer_details_by_prefix($offer_prefix) {
        $db_obj = new DB();
        $offer_sql = "SELECT *
                            FROM offer o inner join hotel h on o.hotel_id=h.hotel_id
                           WHERE o.offer_prefix='".$offer_prefix."'";
        $extra_obj = new Extra();
        $db_obj->query($offer_sql);
        if($db_obj->rowCount() > 0) {
            $offer_details = $extra_obj->objectToArray($db_obj->getRow($offer_sql));
            return $offer_details;
        } else {
            return FALSE;
        }
    }

     public function addofferorder($orderprocessarray) {
         $seq_gateway_order_id=$this->get_seq_gateway_order_id();         
         $orderdetails=array(
             'order_number'=>rand(),
             'offer_id'=>$orderprocessarray["offer_id"],
             'member_id'=>$orderprocessarray["member_id"],
             'creation_date'=>date('Y-m-d H:i:s'),
             'order_status'=>'A',
             'regular_amount'=>$orderprocessarray["regular_amount"],
             'discount_amount'=>$orderprocessarray["discount_amount"],
             'credits_redeemed'=>$orderprocessarray["credits_redeemed"],
             'no_of_offer_vouchers'=>$orderprocessarray["no_of_offer_vouchers"],
             'order_attempts'=>$orderprocessarray["order_attempts"],
             'seq_gateway_order_id'=>$seq_gateway_order_id

         );

        $db_obj = new DB();
        $table = 'offer_orders';
        $db_obj->insert($table, $orderdetails);
        return  $db_obj->getLastInsertId();

    }
/**
 * Updates the offer orders table when an in-complete order is processed
 *
 * @access	public
 * @param       array
 * @param       int
 * 
*/    
    public function update_offer_order($orderprocessarray, $order_id) {
        
        $seq_gateway_order_id=$this->get_seq_gateway_order_id();       
        $orderdetails=array(
             //'order_number'=>rand(),
             //'offer_id'=>$orderprocessarray["offer_id"],
             //'member_id'=>$orderprocessarray["member_id"],
             'creation_date'=>date('Y-m-d H:i:s'),
             'order_status'=>'A',
             'regular_amount'=>$orderprocessarray["regular_amount"],
             'discount_amount'=>$orderprocessarray["discount_amount"],
             'credits_redeemed'=>$orderprocessarray["credits_redeemed"],
             'no_of_offer_vouchers'=>$orderprocessarray["no_of_offer_vouchers"],
             'order_attempts'=>$orderprocessarray["order_attempts"],
             'seq_gateway_order_id'=>$seq_gateway_order_id
         );

        $db_obj = new DB();
        $table = 'offer_orders';
        $where = array("order_id =" . $order_id );
        $db_obj->update($table, $orderdetails, $where);
        

    }
    
     function updateorderstatus($status)
     {
        $updatedetails=array("order_status"=>$status);
        $db_obj = new DB();
        $table = 'offer_orders';
        $data = $updatedetails;
        $where = array("order_id =" . $_SESSION["orderprocessarray"]["orderid"] );
        $db_obj->update($table, $data, $where);
         
     }
     function updateoffertable()
     {
        $updatedetailssql="update offer set maximum_purchases=maximum_purchases-". $_SESSION["orderprocessarray"]["no_of_offer_vouchers"]." where offer_id=".$_SESSION["orderprocessarray"]["offer_id"];
   //  exit($updatedetailssql) ;
        $db_obj = new DB();
        $db_obj->query($updatedetailssql);
        
     }
     
     function get_order_details($orderid){
        $db_obj = new DB();
        $order_sql = "SELECT *
                        FROM offer_orders o
                        INNER JOIN offer f ON o.offer_id = f.offer_id
                        LEFT JOIN offer_images i ON i.offer_id = f.offer_id
                        INNER JOIN member m on m.member_id=o.member_id
                        WHERE o.order_id = '".$orderid."'";
        //echo $order_sql;
        $extra_obj = new Extra();
        $db_obj->query($order_sql);
        if($db_obj->rowCount() > 0) {
            $offer_order_details = $extra_obj->objectToArray($db_obj->getRow($order_sql));
            return $offer_order_details;
        } else {
            return FALSE;
        }
     }
     
/**
 * How many times a particular offer has been ordered by a particular member
 *
 * @access	public
 * @param	int
 * @param	int
 * @return	int	count of the orders
*/
     function offers_ordered_by_a_user($member_id, $offer_id){
        $db_obj = new DB();
        $offer_sql = "SELECT count(*) ordered_offers
                           FROM offer_orders 
                           WHERE offer_id='".$offer_id."' and member_id='".$member_id."' and order_status='C'";
        $extra_obj = new Extra();
        $db_obj->query($offer_sql);        
        $offer_details = $extra_obj->objectToArray($db_obj->getRow($offer_sql));
        return $offer_details['ordered_offers'];
        
     }

/**
 * Genetates a new id, each time a record is sent to the payment gateway.
 *
 * @access	public
 * @return	string	- new sequential id for gateway
*/
     function get_seq_gateway_order_id(){
        $db_obj = new DB();
        //$table = 'sequential_order_numbers';
        $sql="insert into sequential_order_numbers values()";
        $db_obj->query($sql);
        return $db_obj->getLastInsertId();
     }
     
/**
 * Retrieves the stored images for a particular offer
 *
 * @access	public
 * @param	int
 * @return	mixed	
*/
     function get_offer_images($offer_id, $default_img=''){
        $db_obj = new DB();
        $where_part='';
        //$table = 'sequential_order_numbers';
        if (!empty($default_img)) {
            $where_part=" and is_default={$default_img}";
        }
        $sql="select * from offer_images where offer_id={$offer_id} {$where_part} order by is_default";
        
        $extra_obj = new Extra();
        $db_obj->query($sql);
        if($db_obj->rowCount() > 0) {
            return $extra_obj->objectToArray($db_obj->getResults($sql));            
        } else {
            return FALSE;
        }
        /*$db_obj->query($sql);
        return $db_obj->getLastInsertId();*/
     }     
     
     public function create_offer_images($offer_image_details) {
        $db_obj = new DB();
        $table = 'offer_images';
        $db_obj->insert($table, $offer_image_details);
        //return  $db_obj->getLastInsertId();
    }
    
    public function is_valid_order($orderid, $member_id=0){
        $db_obj = new DB();
        $order_sql = "SELECT *
                        FROM `offer_orders` 
                        WHERE order_id = '".$orderid."' and member_id='{$member_id}'";
        //echo $order_sql;        
        $db_obj->query($order_sql);
        if($db_obj->rowCount() > 0) {
            return true;            
        } else {
            return FALSE;
        }
                
    }
}
?>
